package com.wjs.blog.service.Imp;

import com.alibaba.fastjson.JSON;
import com.mysql.cj.util.StringUtils;
import com.wjs.blog.dao.pojo.SysUser;
import com.wjs.blog.service.TokenService;
import com.wjs.blog.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class TokenServiceImp implements TokenService {
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public SysUser checktoken(String token) {
        if(StringUtils.isNullOrEmpty(token)){
            return null;
        }
        Map<String, Object> map = JWTUtils.checkToken(token);
        if(map==null){
            return null;
        }
        String userJson = (String) redisTemplate.opsForValue().get("token_" + token);
        if(StringUtils.isNullOrEmpty(userJson)){
            return null;
        }
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        return sysUser;
    }
}
